package com.mindata.blockchain.core.controller;

import com.google.common.collect.Maps;
import com.mindata.blockchain.block.Instruction;
import com.mindata.blockchain.block.Operation;
import com.mindata.blockchain.common.FastJsonUtil;
import com.mindata.blockchain.core.bean.BaseData;
import com.mindata.blockchain.core.bean.ResultGenerator;
import com.mindata.blockchain.core.manager.OrderManager;
import com.mindata.blockchain.core.manager.PayManager;
import com.mindata.blockchain.core.manager.TicketManager;
import com.mindata.blockchain.core.model.OrderEntity;
import com.mindata.blockchain.core.model.PayEntity;
import com.mindata.blockchain.core.model.TicketEntity;
import com.mindata.blockchain.core.requestbody.BlockRequestBody;
import com.mindata.blockchain.core.requestbody.InstructionBody;
import com.mindata.blockchain.core.service.BlockService;
import com.mindata.blockchain.core.service.InstructionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 王鹏
 */
@Api(tags = "票", description = "票相关业务接口")
@RestController
@RequestMapping("/ticket")
public class TicketController {

    @Resource
    private InstructionService instructionService;

    @Resource
    private BlockService blockService;


    @Resource
    private TicketManager ticketManager;

    @Resource
    private OrderManager orderManager;

    @Resource
    private PayManager payManager;

    @Value("${publicKey:A8WLqHTjcT/FQ2IWhIePNShUEcdCzu5dG+XrQU8OMu54}")
    private String publicKey;
    @Value("${privateKey:yScdp6fNgUU+cRUTygvJG4EBhDKmOMRrK4XJ9mKVQJ8=}")
    private String privateKey;

    @RequestMapping("/generate")
    @ApiOperation(value = "生成票-景区系统使用", notes = "生成批量票", httpMethod = "GET", response = BaseData.class)
    public BaseData generateTicket() throws Exception {

        BlockRequestBody blockRequestBody = new BlockRequestBody();
        blockRequestBody.setPublicKey(publicKey);
        com.mindata.blockchain.block.BlockBody blockBody = new com.mindata.blockchain.block.BlockBody();

        List<Instruction> instructionList = Lists.newArrayList();
        for (int i = 0; i < 50; i++) {
            InstructionBody instructionBody = new InstructionBody();
            instructionBody.setOperation(Operation.ADD);
            instructionBody.setTable("ticket");

            Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
            map.put("sceneryName", "华谊兄弟电影乐园");
            map.put("ticketName", "成人票");
            map.put("status", 0);
            map.put("price",80);
            instructionBody.setJson(FastJsonUtil.toJSONString(map));
            instructionBody.setPublicKey(publicKey);
            instructionBody.setPrivateKey(privateKey);
            Instruction instruction = instructionService.build(instructionBody);
            instructionList.add(instruction);
        }
        blockBody.setInstructions(instructionList);
        blockRequestBody.setBlockBody(blockBody);

        return ResultGenerator.genSuccessResult(blockService.addBlock(blockRequestBody));
    }

    @RequestMapping("/ticketInfo")
    @ApiOperation(value = "票信息接口---前端使用", notes = "获取票信息", httpMethod = "GET", response = BaseData.class)
    public BaseData ticketInfo(){
        TicketEntity ticket = ticketManager.findOne();
        Map<String,Object> resp = Maps.newHashMapWithExpectedSize(3);
        resp.put("sceneryName",ticket.getSceneryName());
        resp.put("ticketName",ticket.getTicketName());
        resp.put("price",ticket.getPrice());
        return ResultGenerator.genSuccessResult(resp);
    }

    @RequestMapping("/getAllTickets")
    @ApiOperation(value = "票列表", notes = "票列表", httpMethod = "GET", response = BaseData.class)
    public BaseData getAllTickets(@RequestParam("status")Integer status) {
        List<TicketEntity> all = ticketManager.findAll(status);
        return ResultGenerator.genSuccessResult(all);
    }


    @RequestMapping("/sendTicket")
    @ApiOperation(value = "出票接口",notes = "出票",httpMethod = "GET",response = BaseData.class)
    public BaseData sendTicket(@ApiParam("ticketId")@RequestParam("ticketId")String ticketId) throws Exception {
        OrderEntity orderEntity = orderManager.findByTicketId(ticketId);
        PayEntity payEntity = payManager.findByOrderId(orderEntity.getOrderId());
        TicketEntity ticket = ticketManager.findByTicketId(ticketId);

        // 获取共享账本里面的数据，确定数据已经完成
        if(orderEntity!=null&&payEntity!=null){

            BlockRequestBody blockRequestBody = new BlockRequestBody();
            blockRequestBody.setPublicKey(publicKey);
            com.mindata.blockchain.block.BlockBody blockBody = new com.mindata.blockchain.block.BlockBody();
            List<Instruction> instructionList = Lists.newArrayList();

            InstructionBody instructionBody = new InstructionBody();
            instructionBody.setOperation(Operation.UPDATE);
            instructionBody.setTable("ticket");
            instructionBody.setInstructionId(ticketId);

            Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
            map.put("sceneryName", ticket.getSceneryName());
            map.put("ticketName", ticket.getTicketName());
            map.put("status", 2);
            map.put("price",80);
            instructionBody.setOldJson(FastJsonUtil.toJSONString(ticket));
            instructionBody.setJson(FastJsonUtil.toJSONString(map));
            instructionBody.setPublicKey(publicKey);
            instructionBody.setPrivateKey(privateKey);
            Instruction instruction = instructionService.build(instructionBody);
            instructionList.add(instruction);
            blockBody.setInstructions(instructionList);
            blockRequestBody.setBlockBody(blockBody);

            return ResultGenerator.genSuccessResult(blockService.addBlock(blockRequestBody));
        }

        return ResultGenerator.genFailResult("错误");




    }



}
